package com.quectel.cms.controller.grid.helper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.cms.interceptor.LoginUserSession;
import com.quectel.cms.util.RegionUtils;
import com.quectel.cms.util.SessionHolder;
import com.quectel.constant.core.grid.GridConstant;
import com.quectel.constant.global.StatisticsConstants;
import com.quectel.constant.sys.SysConstants;
import com.quectel.core.module.grid.dto.GridDto;
import com.quectel.core.module.grid.dto.GridEventRecordDto;
import com.quectel.core.module.grid.dto.GridNoticeRelationDto;
import com.quectel.core.module.grid.dto.GridRegionInfoDto;
import com.quectel.core.module.grid.service.GridService;
import com.quectel.core.module.group.dto.SysGroupRegionDto;
import com.quectel.core.module.group.dto.SysGroupVillageDto;
import com.quectel.core.module.group.service.SysGroupRegionService;
import com.quectel.core.module.group.service.SysGroupVillageService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.sys.module.sys.dto.SysGroupDto;
import com.quectel.sys.module.sys.service.SysGroupService;
import com.quectel.util.common.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/2/7 13:12
 * @description 网格相关帮助类 主要用于
 */
@Component
@Slf4j
public class GridHelper {
    @DubboReference
    private VillageService villageService;
    @DubboReference
    private SysGroupService sysGroupService;
    @DubboReference
    private SysGroupRegionService sysGroupRegionService;
    @DubboReference
    private SysGroupVillageService sysGroupVillageService;
    @DubboReference
    private GridService gridService;

    public GridRegionInfoDto packRegionInfo(Byte regionLevel, String regionCode) {

        String name = null;
        String code = null;
        Byte level = null;

        if (GridConstant.RegionLevel.DISTRICT_LEVEL.getLevel().equals(regionLevel)) {
            RegionUtils.ReignStruct byCode = RegionUtils.getByCode(regionCode);
            if (byCode != null) {
                name = byCode.getName();
                code = byCode.getCode();
                level = GridConstant.RegionLevel.DISTRICT_LEVEL.getLevel();
            }

        }

        if (GridConstant.RegionLevel.STREET_LEVEL.getLevel().equals(regionLevel)) {
            RegionUtils.ReignStruct byCode = RegionUtils.getByCode(regionCode);
            if (byCode != null) {
                name = byCode.getName();
                code = byCode.getCode();
                level = GridConstant.RegionLevel.STREET_LEVEL.getLevel();
            }

        }
        if (GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel().equals(regionLevel)) {
            VillageDto villageDto = villageService.selectCacheById(Long.valueOf(regionCode));
            if (villageDto != null) {
                name = villageDto.getName();
                code = String.valueOf(villageDto.getId());
                level = GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel();
            }
        }

        if (GridConstant.RegionLevel.GRID_LEVEL.getLevel().equals(regionLevel)) {
            GridDto gridDto = gridService.selectCacheById(Long.valueOf(regionCode));
            if (gridDto != null) {
                name = gridDto.getVillage() != null ? gridDto.getVillage().getName() + gridDto.getName() : "";
                code = String.valueOf(gridDto.getId());
                level = GridConstant.RegionLevel.GRID_LEVEL.getLevel();
            }
        }
        return GridRegionInfoDto.builder().level(level).name(name).code(code).build();
    }


    /**
     * 获得上级派发事件参数
     *
     * @param params
     * @return
     */
    public Map<String, Object> getGridAssignParams(Map<String, Object> params) {
        //查看当前等级
        List<GridRegionInfoDto> gridRegionInfoDtos = queryRegionInfoByGroupId(SessionHolder.getSysUser().getGroupId());
        if (CollectionUtils.isEmpty(gridRegionInfoDtos)) {
            return Collections.emptyMap();
        }

        params.put(GridConstant.REGION_LEVEL, gridRegionInfoDtos.get(0).getLevel());
        //派送事件
        params.put(GridConstant.EVENT_FLOW_TYPE, GridConstant.EVENT_FLOW_TYPE_ASSIGN);
        //如果是街道
        if (GridConstant.RegionLevel.STREET_LEVEL.getLevel().equals(gridRegionInfoDtos.get(0).getLevel())) {
            params.put(LambdaUtil.getFieldName(GridEventRecordDto::getStreetCode), gridRegionInfoDtos.get(0).getCode());
            params.put(GridConstant.STREET_CODES, Collections.singletonList(gridRegionInfoDtos.get(0).getCode()));
        } else if (GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel().equals(gridRegionInfoDtos.get(0).getLevel())) {
            List<String> villageIs = gridRegionInfoDtos.stream().map(GridRegionInfoDto::getCode).collect(Collectors.toList());
            params.put(GridConstant.VILLAGE_IDS, villageIs);
        } else {
            return Collections.emptyMap();
        }
        //后台创建
        params.put(LambdaUtil.getFieldName(GridEventRecordDto::getSourceType), GridConstant.GridEventRecordSourceType.BACKGROUND_CREATION.getType());

        return params;
    }

    /**
     * 获得网格上报事件参数
     *
     * @param params
     * @return
     */
    public Map<String, Object> getGridReportParams(Map<String, Object> params) {
        //查看所属地区
        List<GridRegionInfoDto> gridRegionInfoDtos = queryRegionInfoByGroupId(SessionHolder.getSysUser().getGroupId());

        if (CollectionUtils.isEmpty(gridRegionInfoDtos)) {
            return Collections.emptyMap();
        }

        params.put(GridConstant.REGION_LEVEL, gridRegionInfoDtos.get(0).getLevel());
        //上报事件
        params.put(GridConstant.EVENT_FLOW_TYPE, GridConstant.EVENT_FLOW_TYPE_REPORT);
        //类型为网格员上报
        params.put(LambdaUtil.getFieldName(GridEventRecordDto::getSourceType), GridConstant.GridEventRecordSourceType.REPORT.getType());

        //
        String regionCode = "";
        if (params.get(GridConstant.REGION_CODE) != null) {
            regionCode = String.valueOf(params.get(GridConstant.REGION_CODE));
        }

        Long regionId = null;
        if (StringUtils.isNotEmpty(regionCode)) {
            regionId = Long.valueOf(regionCode);
        }

        //如果是街道
        if (GridConstant.RegionLevel.DISTRICT_LEVEL.getLevel().equals(gridRegionInfoDtos.get(0).getLevel())) {
            params.put(LambdaUtil.getFieldName(GridEventRecordDto::getDistrictCode), gridRegionInfoDtos.get(0).getCode());
            params.put(GridConstant.DISTRICT_CODES, Collections.singletonList(gridRegionInfoDtos.get(0).getCode()));
            params.put(LambdaUtil.getFieldName(GridEventRecordDto::getStreetCode), regionCode);
            //如果是街道
        } else if (GridConstant.RegionLevel.STREET_LEVEL.getLevel().equals(gridRegionInfoDtos.get(0).getLevel())) {
            params.put(LambdaUtil.getFieldName(GridEventRecordDto::getStreetCode), gridRegionInfoDtos.get(0).getCode());
            params.put(GridConstant.STREET_CODES, Collections.singletonList(gridRegionInfoDtos.get(0).getCode()));
            params.put(LambdaUtil.getFieldName(GridEventRecordDto::getVillageId), regionId);
        } else if (GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel().equals(gridRegionInfoDtos.get(0).getLevel())) {
            //如果是社区
            List<String> villageIs = gridRegionInfoDtos.stream().map(GridRegionInfoDto::getCode).collect(Collectors.toList());
            params.put(GridConstant.VILLAGE_IDS, villageIs);
            params.put(LambdaUtil.getFieldName(GridEventRecordDto::getGridId), regionId);
        } else {
            return Collections.emptyMap();
        }
        return params;
    }

    /**
     * 获得后台创建事件参数
     *
     * @param params
     * @return
     */
    public Map<String, Object> getSysCreatedParams(Map<String, Object> params) {
        //查看当前等级
        List<GridRegionInfoDto> gridRegionInfoDtos = queryRegionInfoByGroupId(SessionHolder.getSysUser().getGroupId());
        if (CollectionUtils.isEmpty(gridRegionInfoDtos)) {
            return Collections.emptyMap();
        }

        List<String> creatorUniqueFlags = gridRegionInfoDtos.stream().map(GridRegionInfoDto::getCode).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(creatorUniqueFlags)) {
            params.put(LambdaUtil.getFieldName(GridEventRecordDto::getCreatorLevel), gridRegionInfoDtos.get(0).getLevel());
            params.put(GridConstant.CREATOR_UNIQUE_FLAGS, creatorUniqueFlags);
        }
        String sourceTypeFlag = "";

        //是否指定了sourceTypeFlag
        //后台创建事件或者智能发现
        List<Byte> sourceTypes = new ArrayList<>(2);
        if (params.get(GridConstant.SOURCE_TYPE_FLAG) != null) {
            sourceTypeFlag = String.valueOf(params.get(GridConstant.SOURCE_TYPE_FLAG));
            if (sourceTypeFlag.equals(String.valueOf(GridConstant.GridEventRecordSourceType.BACKGROUND_CREATION.getType()))) {
                sourceTypes.add(GridConstant.GridEventRecordSourceType.BACKGROUND_CREATION.getType());
            }
            if (sourceTypeFlag.equals(String.valueOf(GridConstant.GridEventRecordSourceType.INTELLIGENT_DISCOVERY.getType()))) {
                sourceTypes.add(GridConstant.GridEventRecordSourceType.INTELLIGENT_DISCOVERY.getType());
            }
        } else {

            sourceTypes.add(GridConstant.GridEventRecordSourceType.BACKGROUND_CREATION.getType());
            sourceTypes.add(GridConstant.GridEventRecordSourceType.INTELLIGENT_DISCOVERY.getType());

        }
        params.put(GridConstant.SOURCE_TYPES, sourceTypes);


        return params;
    }


    /**
     * 层级数组
     */
    public static final String[] LEVEL_FIELD_ARR = {
            LambdaUtil.getFieldName(GridEventRecordDto::getProvinceCode),
            LambdaUtil.getFieldName(GridEventRecordDto::getCityCode),
            LambdaUtil.getFieldName(GridEventRecordDto::getDistrictCode),
            LambdaUtil.getFieldName(GridEventRecordDto::getStreetCode),
            LambdaUtil.getFieldName(GridEventRecordDto::getVillageId),
            LambdaUtil.getFieldName(GridEventRecordDto::getGridId),
    };


    /**
     * 层级数组
     */
    public static final Byte[] LEVEL_ARR = {
            GridConstant.RegionLevel.PROVINCE_LEVEL.getLevel(),
            GridConstant.RegionLevel.CITY_LEVEL.getLevel(),
            GridConstant.RegionLevel.DISTRICT_LEVEL.getLevel(),
            GridConstant.RegionLevel.STREET_LEVEL.getLevel(),
            GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel(),
            GridConstant.RegionLevel.GRID_LEVEL.getLevel()
    };

    /**
     * 获取移动后的地域信息 左移减 右移加
     *
     * @param dto
     * @return
     */
    public GridRegionInfoDto shiftRegionLevel(GridEventRecordDto dto, int offset) {
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(dto);
        //当前等级
        if (dto == null || dto.getCurrentLevel() == null) {
            return null;
        }
        int index = dto.getCurrentLevel() - 1 + offset;
        if (index < 0) {
            index = 0;
        }
        if (index > LEVEL_FIELD_ARR.length - 1) {
            index = LEVEL_FIELD_ARR.length - 1;
        }

        return GridRegionInfoDto.builder()
                .code(String.valueOf(stringObjectMap.get(LEVEL_FIELD_ARR[index])))
                .level((byte) (index + 1))
                .build();

    }

    /**
     * 查看当前事件的最高层级
     *
     * @param dto
     * @return
     */
    public Byte queryGridEventSuperLevel(GridEventRecordDto dto) {
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(dto);
        //判断创建层级
        Byte creatorLevel = dto.getCreatorLevel();
        //创建等级肯定在两端
        //如果下一级不为空则继续查询下一级 知道找到最后一个为空的,则上一个层级即为当前事件所在层级 相反也一样
        //初始层级是创建层级
        int regionLevel = creatorLevel - 1;

        while (regionLevel >= 0) {
            if (regionLevel == 0) {
                return LEVEL_ARR[0];
            }
            if (stringObjectMap.get(LEVEL_FIELD_ARR[regionLevel]) != null) {
                regionLevel--;
            } else {
                return LEVEL_ARR[regionLevel + 1];
            }
        }
        return LEVEL_ARR[regionLevel];
    }

    public static void main(String[] args) {

        GridEventRecordDto gridEventRecordDto = new GridEventRecordDto();
        gridEventRecordDto.setProvinceCode("省");
        gridEventRecordDto.setCityCode("市");
        gridEventRecordDto.setDistrictCode("区");
        gridEventRecordDto.setStreetCode("街道");
        gridEventRecordDto.setVillageId(11111L);
        gridEventRecordDto.setGridId(2222L);

        gridEventRecordDto.setCurrentLevel(GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel());
        GridHelper gridHelper = new GridHelper();
        GridRegionInfoDto regionInfoDto = gridHelper.shiftRegionLevel(gridEventRecordDto, -5);
        System.out.println(JacksonUtils.toJsonString(regionInfoDto));

    }


    /**
     * 根据groupId 获取当前用户所在用户组的信息
     * 社区下面可能是多个小区
     * 所以返回list
     *
     * @param groupId
     */
    public List<GridRegionInfoDto> queryRegionInfoByGroupId(Long groupId) {
        SysGroupDto sysGroupDto = sysGroupService.selectById(groupId);
        //如果是区域组
        if (SysConstants.GroupAuthEnum.BY_REGION.getType().equals(sysGroupDto.getAuthorityType())) {
            SysGroupRegionDto sysGroupRegionDto = sysGroupRegionService.queryOneByGroupId(groupId);
            if (sysGroupRegionDto == null) {
                return Collections.emptyList();
            }
            RegionUtils.ReignStruct reignStruct = RegionUtils.getByCode(sysGroupRegionDto.getCode());
            if (reignStruct == null) {
                return Collections.emptyList();
            }
            //省 当前没有网格
            if (Integer.valueOf(SysConstants.RegionLevelEnum.PROVINCE_LEVEL.getLevel()).equals(sysGroupRegionDto.getType())) {
                return Collections.emptyList();
            }
            //市 当前没有网格
            if (Integer.valueOf(SysConstants.RegionLevelEnum.CITY_LEVEL.getLevel()).equals(sysGroupRegionDto.getType())) {
                return Collections.emptyList();
            }
            //区
            if (Integer.valueOf(SysConstants.RegionLevelEnum.DISTRICT_LEVEL.getLevel()).equals(sysGroupRegionDto.getType())) {
                return Collections.singletonList(GridRegionInfoDto.builder()
                        .level(GridConstant.RegionLevel.DISTRICT_LEVEL.getLevel())
                        .name(reignStruct.getName())
                        .code(reignStruct.getCode())
                        .parentCode(reignStruct.getParentCode())
                        .parentName(reignStruct.getParentName())
                        .build());
            }
            //街道
            if (Integer.valueOf(SysConstants.RegionLevelEnum.STREET_LEVEL.getLevel()).equals(sysGroupRegionDto.getType())) {
                return Collections.singletonList(GridRegionInfoDto.builder()
                        .level(GridConstant.RegionLevel.STREET_LEVEL.getLevel())
                        .name(reignStruct.getName())
                        .code(reignStruct.getCode())
                        .parentCode(reignStruct.getParentCode())
                        .parentName(reignStruct.getParentName())
                        .build());
            }
        }
        //如果是社区组,查询授权小区信息
        if (SysConstants.GroupAuthEnum.BY_VILLAGE.getType().equals(sysGroupDto.getAuthorityType())) {
            //查询社区组下面的全部小区
            List<SysGroupVillageDto> sysGroupVillageDtos = sysGroupVillageService.queryListByGroupId(groupId);

            if (CollectionUtils.isNotEmpty(sysGroupVillageDtos)) {
                return sysGroupVillageDtos.stream().map(sysGroupVillageDto -> {
                    VillageDto villageDto = villageService.selectById(sysGroupVillageDto.getVillageId());
                    if (villageDto == null) {
                        return null;
                    }
                    return GridRegionInfoDto.builder()
                            .level(GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel())
                            .name(villageDto.getName())
                            .code(String.valueOf(villageDto.getId()))
                            .parentName(villageDto.getStreetName())
                            .parentCode(villageDto.getStreetCode())
                            .build();

                }).filter(Objects::nonNull).collect(Collectors.toList());
            }

        }
        //如果是楼栋组
        //网格最小单位为小区,该用户组没有网格权限
        return Collections.emptyList();
    }

    /**
     * 根据groupId 获取当前用户子级用户组的信息
     *
     * @param groupId
     */
    public List<GridRegionInfoDto> queryChildRegionInfoByGroupId(Long groupId) {
        SysGroupDto sysGroupDto = sysGroupService.selectById(groupId);
        //如果是区域组
        if (SysConstants.GroupAuthEnum.BY_REGION.getType().equals(sysGroupDto.getAuthorityType())) {
            SysGroupRegionDto sysGroupRegionDto = sysGroupRegionService.queryOneByGroupId(groupId);
            if (sysGroupRegionDto == null) {
                return Collections.emptyList();
            }
            RegionUtils.ReignStruct byCode = RegionUtils.getByCode(sysGroupRegionDto.getCode());
            List<RegionUtils.ReignStruct> byParentCode = RegionUtils.getByParentCode(sysGroupRegionDto.getCode());

            //查询子级用户组的code
            if (byCode == null) {
                return Collections.emptyList();
            }
            //省 当前没有网格
            if (Integer.valueOf(SysConstants.RegionLevelEnum.PROVINCE_LEVEL.getLevel()).equals(sysGroupRegionDto.getType())) {
                return Collections.emptyList();
            }
            //市 下一级是区网格
            if (Integer.valueOf(SysConstants.RegionLevelEnum.CITY_LEVEL.getLevel()).equals(sysGroupRegionDto.getType())) {
                return Collections.emptyList();
            }
            //区 下一级是街道网格
            if (CollectionUtils.isNotEmpty(byParentCode) && Integer.valueOf(SysConstants.RegionLevelEnum.DISTRICT_LEVEL.getLevel()).equals(sysGroupRegionDto.getType())) {
                return byParentCode.stream().map(reignStruct -> {
                    SysGroupRegionDto byTenantAndCode = sysGroupRegionService.queryOneByTenantAndRQ(sysGroupDto.getTenantId(), reignStruct.getCode());
                    if (byTenantAndCode != null) {
                        return GridRegionInfoDto.builder()
                                .level(GridConstant.RegionLevel.STREET_LEVEL.getLevel())
                                .name(reignStruct.getName())
                                .code(reignStruct.getCode())
                                .parentCode(byCode.getCode())
                                .parentName(byCode.getName())
                                .build();
                    } else {
                        return null;
                    }
                }).filter(Objects::nonNull).collect(Collectors.toList());
            }
            //街道 下一级是小区
            if (Integer.valueOf(SysConstants.RegionLevelEnum.STREET_LEVEL.getLevel()).equals(sysGroupRegionDto.getType())) {

                Map<String, Object> params = new HashMap<>(2);
                Map<String, Object> loginUserSession = new HashMap<>(2);
                loginUserSession.put(LambdaUtil.getFieldName(LoginUserSession::getTenantId), sysGroupDto.getTenantId());
                params.put("_loginUserSession", loginUserSession);
                params.put("_sysGroup", sysGroupDto);
                List<VillageDto> list = villageService.queryList(params);

                return list.stream().map(villageDto -> GridRegionInfoDto.builder()
                        .level(GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel())
                        .name(villageDto.getName())
                        .code(String.valueOf(villageDto.getId()))
                        .parentCode(byCode.getCode())
                        .parentName(byCode.getName())
                        .build()).collect(Collectors.toList());
            }
        }
        //如果是社区组,查询对应的全部网格
        if (SysConstants.GroupAuthEnum.BY_VILLAGE.getType().equals(sysGroupDto.getAuthorityType())) {
            List<GridRegionInfoDto> gridRegionInfoDtos = new ArrayList<>();
            //查询社区组下面的全部小区
            List<SysGroupVillageDto> sysGroupVillageDtos = sysGroupVillageService.queryListByGroupId(groupId);
            if (CollectionUtils.isNotEmpty(sysGroupVillageDtos)) {
                for (SysGroupVillageDto sysGroupVillageDto : sysGroupVillageDtos) {
                    VillageDto villageDto = villageService.selectById(sysGroupVillageDto.getVillageId());
                    if (villageDto == null) {
                        continue;
                    }
                    //查询社区下面所有的网格
                    Map<String, Object> params = new HashMap<>(2);
                    params.put(LambdaUtil.getFieldName(GridDto::getTenantId), villageDto.getTenantId());
                    params.put(LambdaUtil.getFieldName(GridDto::getVillageId), villageDto.getId());
                    List<GridDto> gridDtos = gridService.queryList(params);
                    if (CollectionUtils.isNotEmpty(gridDtos)) {
                        for (GridDto gridDto : gridDtos) {
                            gridRegionInfoDtos.add(
                                    GridRegionInfoDto
                                            .builder()
                                            .level(GridConstant.RegionLevel.GRID_LEVEL.getLevel())
                                            //带上小区名称用"-"隔开
                                            .name(villageDto.getName() + gridDto.getName())
                                            .code(String.valueOf(gridDto.getId()))
                                            .parentCode(String.valueOf(villageDto.getId()))
                                            .parentName(villageDto.getName())
                                            .build());
                        }
                    }
                }

            }
            return gridRegionInfoDtos;

        }
        return Collections.emptyList();
    }

    /**
     * 判断是否有上级通知
     * 当前网格只有街道/社区/网格才有上级通知
     *
     * @param gridRegionInfoDtos
     * @return
     */

    public Boolean checkIfExistSuperiorNotice(List<GridRegionInfoDto> gridRegionInfoDtos) {
        for (GridRegionInfoDto gridRegionInfoDto : gridRegionInfoDtos) {
            //街道/社区/网格有上级通知
            if (GridConstant.RegionLevel.STREET_LEVEL.getLevel().equals(gridRegionInfoDto.getLevel())
                    || GridConstant.RegionLevel.VILLAGE_LEVEL.getLevel().equals(gridRegionInfoDto.getLevel())
                    || GridConstant.RegionLevel.GRID_LEVEL.getLevel().equals(gridRegionInfoDto.getLevel())
            ) {
                return Boolean.TRUE;
            }

        }
        return Boolean.FALSE;
    }

    /**
     * 判断通知公告是否能下发到下属机构
     *
     * @param groupId
     * @param relationList
     * @return
     */
    public Boolean checkIfExistChildRegion(Long groupId, List<GridNoticeRelationDto> relationList) {

        //查询下属单位 判断绑定关系是否存在于关系列表中
        List<GridRegionInfoDto> gridRegionInfoDtos = queryChildRegionInfoByGroupId(groupId);
        if (CollectionUtils.isEmpty(gridRegionInfoDtos)) {
            return Boolean.FALSE;
        }
        Set<String> collect = gridRegionInfoDtos.stream().map(GridRegionInfoDto::getCode).filter(Objects::nonNull).collect(Collectors.toSet());
        Byte leve = gridRegionInfoDtos.get(0).getLevel();
        for (GridNoticeRelationDto gridNoticeRelationDto : relationList) {
            if (!collect.contains(gridNoticeRelationDto.getSourceId())) {
                return Boolean.FALSE;
            }
            //设置绑定关系
            gridNoticeRelationDto.setSourceType(leve);
        }
        return Boolean.TRUE;
    }


    public Pair<String, String> getStartAndEndTime(String type) {
        DateTime now = DateTime.now();
        String startDate = null;
        String endDate = null;
        switch (type) {
            case StatisticsConstants.TIME_RANGE_BY_WEEK:
                startDate = DateUtil.format(DateUtil.beginOfWeek(now), DatePattern.NORM_DATETIME_PATTERN);
                endDate = DateUtil.format(DateUtil.endOfWeek(now), DatePattern.NORM_DATETIME_PATTERN);
                break;
            case StatisticsConstants.TIME_RANGE_BY_MONTH:
                startDate = DateUtil.format(DateUtil.beginOfMonth(now), DatePattern.NORM_DATETIME_PATTERN);
                endDate = DateUtil.format(DateUtil.endOfMonth(now), DatePattern.NORM_DATETIME_PATTERN);
                break;
            case StatisticsConstants.TIME_RANGE_BY_DAY:
            default:
                startDate = DateUtil.format(DateUtil.beginOfDay(now), DatePattern.NORM_DATETIME_PATTERN);
                endDate = DateUtil.format(DateUtil.endOfDay(now), DatePattern.NORM_DATETIME_PATTERN);
        }
        return MutablePair.of(startDate, endDate);
    }


}
